package jonaPad;

import javax.swing.JTextArea;
import javax.swing.JList;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.JTextPane;
import java.awt.Color;
import java.awt.event.*;

import jonaArgumentationSchemes.Knowledge;
import Jona.*;



public class ModelJTextArea extends JTextPane implements ActionListener,CaretListener{
	static final long serialVersionUID = 102032;
	int oldLine = 0;
	JTextArea jta1,jta2;
	GTimer gt1;
	boolean changed = false;
	ArgModelChecker2 amc1;
	private Theory activeTheory;
	
	public ModelJTextArea(JTextArea jta1, JTextArea jta2)
	{
		super();
		activeTheory= null;
		 
		amc1 = new ArgModelChecker2();
		this.jta1 = jta1;
		this.jta2 = jta2;
		gt1 = new GTimer(1000,this);
		//setEditorKit(new HTMLEditorKit());
		addCaretListener(this);
		//getStyledDocument()
		Style style = addStyle("purple", null);
	    StyleConstants.setForeground(style, new Color(128,0,128));
	    StyleConstants.setFontSize(style,12);
	    StyleConstants.setBold(style,true);
		
	    style = addStyle("blue", null);
	    StyleConstants.setForeground(style, Color.BLUE);
	    StyleConstants.setFontSize(style,12);
	    StyleConstants.setBold(style,true);
	    
	    style = addStyle("Black", null);
	    StyleConstants.setForeground(style, Color.BLACK);
	    StyleConstants.setFontSize(style,12);
	    
		gt1.start();
	}
	
	
	public void actionPerformed(ActionEvent e)
	{
		if(changed)
		{
			checkDocument();
			
	        // Suggestions.
			Knowledge.suggest(getText());
		}
		changed = false;
	
	}
	
	public Theory getActiveTheory()
	{
		checkDocument();
		return activeTheory;
	}
	
	public void setTheory(Theory t1)
	{
		
		activeTheory = t1;
		String rt ="";
		//char c1 = 97;
		//char c2 = 97;
		//String id = "";
		
		//still to implement
		for(int i =0; i < t1.getStatements().length; i++)
		{
			if(t1.getStatements()[i].getClass().getSimpleName().equals("Statement"))
			{				
				
				rt += t1.getStatements()[i].getName() + ": \""  + t1.getStatements()[i].getText() + "\"" + " as " + giveArgumentType(t1.getStatements()[i].getArgumentType()) + "\n"; 
			}
		}
		for(int i =0; i < t1.getStatements().length; i++)
		{
			if(t1.getStatements()[i].getClass().getSimpleName().equals("Conditional"))
			{
				Conditional con1 = (Conditional)t1.getStatements()[i];
				rt +=  con1.getName() + ": if(";
				for(int j =0; j < con1.getAntecedents().length-1 ; j++)
				{
					rt += con1.getAntecedents()[j].getName() + ",";
				}
					rt += con1.getAntecedents()[con1.getAntecedents().length-1].getName() + ") then " + con1.getConsequent().getName() + " as " + giveArgumentType(t1.getStatements()[i].getArgumentType()) + "\n";
				//t1.getStatements()[i].name = "" +c1;
				//c1++;	
			}
		}	
		setText(rt);
		//highlight(this,"issue","purple");
		//highlight(this,"as", "blue");	
		//highlight(this,"assumption","purple");
		//highlight(this,"undefined", "purple");
		//highlight(this,"if", "blue");
		//highlight(this,"then", "blue");	
	}
	
	private String giveArgumentType(int type)
	{
		String rt ="";
	
		switch(type)
		{
			case ArgObject.UNDEFINED:
				rt = "undefined";
			break;
			case ArgObject.ISSUE:
				rt = "issue";
			break;
			case ArgObject.ASSUMPTION:
				rt = "assumption";
			break;

		}
		return rt;
	}
	
	private void checkDocument()
	{
		boolean errorFound = false;
		Theory t1 = null;
		jta2.setText("No problems found...");
		try{
			t1 =amc1.checkModel(getText());
		}
		catch(ModelException me)
		{
			jta2.setText(me.getMessage());
			errorFound = true;
			//System.out.println(me.getMessage());
		}
		if(!errorFound) 
		{
			activeTheory = t1;
			jta1.setText(t1.toString());
		}
		try{		
		  Document doc = 	getDocument();
          getStyledDocument().setCharacterAttributes(0,doc.getLength(), getStyle("Black"), true);
			}
		catch(Exception e) {}
		
		//checkInput();
		highlight(this,"issue","purple");
		highlight(this,"as", "blue");	
		highlight(this,"assumption","purple");
		highlight(this,"undefined", "purple");
		highlight(this,"if", "blue");
		highlight(this,"then", "blue");	
	}
	// JTextArea textComp = new JTextArea();
	    
	    // Highlight the occurrences of the word "public"
	   
	    
	    // Creates highlights around all occurrences of pattern in textComp
	    public void highlight(JTextComponent textComp, String pattern, String style) {
	        // First remove all old highlights
	        //removeHighlights(textComp);
	    	//System.out.println("changed");
	        try {
	  		  Document doc = textComp.getDocument();
	          String text = doc.getText(0, doc.getLength()).toLowerCase();
	          int pos = 0;
	         // getStyledDocument().setCharacterAttributes(0,doc.getLength(), getStyle("Black"), true);
	            // Search for pattern
	            while ((pos = text.indexOf(pattern, pos)) >= 0) {
	                // Create highlighter using private painter and apply around pattern
	            	//System.out.println("pattern found");
	            	getStyledDocument().setCharacterAttributes(pos,pattern.length(), getStyle(style), true);
	            	//hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
	                pos += pattern.length();
	            }
	            getStyledDocument().setCharacterAttributes(pos,1, getStyle("Black"), true);
	            
	        } catch (BadLocationException e) {
	        	System.out.println(e.getMessage());
	        }
	        	
	        	

	    }
	    

	public void caretUpdate(CaretEvent e)
	{	
		changed = true;
	}
	
}

